package com.common.stdlib
{
	import com.global.GlobalDef;
	
	import flash.utils.*;
	
	/**
	 *字符串流 
	 * @author lxh
	 * 
	 */	
	public class StringUtil
	{
		public function StringUtil()
		{
		}

		/**
		 *写入文本到内存字节流 
		 * @param dst
		 * @param val
		 * @param length
		 * @return 
		 * 
		 */		
		public static function writeStringToByteArray(dst:ByteArray,val:String,length:uint=0):uint
		{
			var tmp:ByteArray = ArrayUtil.newLitteEndianByteArray();
			tmp.writeMultiByte(val, "utf-8");
			if(length != 0)
				tmp.length = length;
			dst.writeBytes(tmp);
			
			return tmp.length;
		}
		/**
		 *读取文本从内存字节流 
		 * @param dst
		 * @param length
		 * @return 
		 * 
		 */		
		public static function readStringByByteArray(dst:ByteArray,length:uint):String
		{
			var tmp:ByteArray = ArrayUtil.newLitteEndianByteArray();
			dst.readBytes(tmp, 0, length);		
			return tmp.readMultiByte(length, "utf-8");
		}
		/**
		 *获取文本字节长度 
		 * @param val
		 * @return 
		 * 
		 */		
		public static function getStringByteLength(val:String):uint
		{
			var tmp:ByteArray = ArrayUtil.newLitteEndianByteArray();
			tmp.writeMultiByte(val, "utf-8");
			return tmp.length;
		}
		
		/**
		 *	Removes whitespace from the front and the end of the specified
		 *	string.
		 * 
		 *	@param input The String whose beginning and ending whitespace will
		 *	will be removed.
		 *
		 *	@returns A String with whitespace removed from the begining and end	
		 *
		 * 	@langversion ActionScript 3.0
		 *	@playerversion Flash 9.0
		 *	@tiptext
		 */			
		public static function trim(input:String):String
		{
			return StringUtil.ltrim(StringUtil.rtrim(input));
		}
		
		/**
		 *	Removes whitespace from the front of the specified string.
		 * 
		 *	@param input The String whose beginning whitespace will will be removed.
		 *
		 *	@returns A String with whitespace removed from the begining	
		 *
		 * 	@langversion ActionScript 3.0
		 *	@playerversion Flash 9.0
		 *	@tiptext
		 */	
		public static function ltrim(input:String):String
		{
			var size:Number = input.length;
			for(var i:Number = 0; i < size; i++)
			{
				if(input.charCodeAt(i) > 32)
				{
					return input.substring(i);
				}
			}
			return "";
		}
		
		/**
		 *	Removes whitespace from the end of the specified string.
		 * 
		 *	@param input The String whose ending whitespace will will be removed.
		 *
		 *	@returns A String with whitespace removed from the end	
		 *
		 * 	@langversion ActionScript 3.0
		 *	@playerversion Flash 9.0
		 *	@tiptext
		 */	
		public static function rtrim(input:String):String
		{
			var size:Number = input.length;
			for(var i:Number = size; i > 0; i--)
			{
				if(input.charCodeAt(i - 1) > 32)
				{
					return input.substring(0, i);
				}
			}
			
			return "";
		}
		/**
		 * filter worlds ...
		 * @param content
		 * @return 
		 * 
		 */	
		private static var tempStr :String;
		public static function filterWord(content :String):String
		{
			tempStr = content;
			GlobalDef.filterWords.forEach(filterFunc);
			return tempStr;
		}
		private static function filterFunc(item:String, index:int, array:Array):void
		{
			if(item == null)
			{
				return ;
			}
			var regx :RegExp = new RegExp(item,"gi");
			var rplS :String = "";
			for (var i :int = 0;i < item.length;i++)
			{
				rplS += "*";
			}
			tempStr = tempStr.replace(regx,rplS);
		}
	}
}